/*
** case.c for  in /u/all/tessa_b/cu/public/trantor
** 
** Made by arnaud1 tessa
** Login   <tessa_b@epitech.net>
** 
** Started on  Tue Jul  5 09:56:33 2011 arnaud1 tessa
** Last update Sun Jul 10 12:57:49 2011 mouhamed faye
*/

#include	<stdio.h>
#include	<time.h>
#include	<stdlib.h>
#include	<string.h>
#include	<strings.h>
#include	"case.h"

void		case_init(t_case *c, int max_trantorians)
{
  t_trantorian	**tr;
  int		size;

  size = (max_trantorians + 1) * sizeof(*tr);
  tr = malloc(size);
  if (tr == NULL)
    {
      fprintf(stderr, "case_init : out of memory\n");
      exit(1);
    }
  bzero(tr, size);
  c->trantorians = tr;
  c->nb_trantorians = 0;
  resources_init(&(c->resources));
}

int		case_remove_resource(t_case *c,
				  const char *r_name)
{
  int		res;

  if (!strcmp("nourriture", r_name))
    res = case_remove_nourriture(c);
  else if (!strcmp("linemate", r_name))
    res = case_remove_linemate(c);
  else if (!strcmp("deraumere", r_name))
    res = case_remove_deraumere(c);
  else if (!strcmp("sibur", r_name))
    res = case_remove_sibur(c);
  else if (!strcmp("mendiane", r_name))
    res = case_remove_mendiane(c);
  else if (!strcmp("phiras", r_name))
    res = case_remove_phiras(c);
  else if (!strcmp("thystame", r_name))
    res = case_remove_thystame(c);
  else
  res = 0;
  return (res);
}

int		case_add_resource(t_case *c,
				   const char *r_name)
{
  int		res;

  res = 1;
  if (!strcmp("nourriture", r_name))
    case_add_nourriture(c);
  else if (!strcmp("linemate", r_name))
    case_add_linemate(c);
  else if (!strcmp("deraumere", r_name))
    case_add_deraumere(c);
  else if (!strcmp("sibur", r_name))
    case_add_sibur(c);
  else if (!strcmp("mendiane", r_name))
    case_add_mendiane(c);
  else if (!strcmp("phiras", r_name))
    case_add_phiras(c);
  else if (!strcmp("thystame", r_name))
    case_add_thystame(c);
  else
  res = 0;
  return (res);
}

char		*case_to_string(t_case *c)
{
  char		*resources_str;
  char		*case_str;
  int		size;

  resources_str = resources_to_string(&(c->resources));
  size = strlen(resources_str) + 6;
  if (c->nb_trantorians <= 0)
    return (resources_str);
  case_str = malloc(strlen(resources_str + 6));
  if (case_str == NULL)
    {
      fprintf(stderr, "case_to_string : out of memory\n");
      exit(1);
    }
  bzero(case_str, size);
  strcat(case_str, " Joueur");
  strcat(case_str, resources_str);
  return (case_str);
}

int		case_add_trantorian(t_case *c, t_trantorian *tr)
{
  int		i;

  i = 0;
  while (c->trantorians[i] != NULL)
    {
      i++;
    }
  c->trantorians[i] = tr;
  c->nb_trantorians++;
}

void		case_remove_trantorian(t_case *c, t_trantorian *tr)
{
  int		ind;
  int		i;
  int		nb;

  i = 0;
  nb = c->nb_trantorians;
  if (nb > 0)
    {
      while (i < nb)
	{
	  if (c->trantorians[i] == tr)
	    {
	      c->trantorians[i] = c->trantorians[nb - 1];
	      c->trantorians[nb - 1] = NULL;
	      c->nb_trantorians--;
	      break;
	    }
	  i++;
	}
    }
}

/*
int		main(int ac, char **av)
{
  t_case	c;
  t_trantorian	t;

  t.team_name = "epiteK";
  case_init(&c, 42);
  printf("%s\n", case_to_string(&c));
  case_add_resource(&c, "linemate");
  printf("%s\n", case_to_string(&c));
  case_remove_resource(&c, "linemate");
  printf("%s\n", case_to_string(&c));
  case_add_trantorian(&c, &t);
  case_add_trantorian(&c, &t);
  printf("%s\n", (c.trantorians[0])->team_name);
  printf("%s\n", case_to_string(&c));
  case_remove_trantorian(&c, &t);
  case_remove_trantorian(&c, &t);
  printf("%s\n", case_to_string(&c));
}
*/

/*
** cc case*.c ../resources/resources.c 
*/
